package com.manli.api.service;

import com.manli.api.pojo.QueryParamsNoticeYip;
import com.manli.api.pojo.SessionInfo;
import com.manli.api.util.SessionInfoUtils;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.NoticeTipDao;
import com.manli.api.dto.NoticeTip;

import javax.servlet.http.HttpServletRequest;


@Service("noticeTipService")
public class NoticeTipService {
    @Autowired
    NoticeTipDao noticeTipDao;
    @Autowired
    SQLManager sqlManager;
    @Autowired
    SessionInfoUtils sessionInfoUtils;

    
    public Result queryPage(QueryParamsNoticeYip params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from notice_tip where 1=1 ");
        sql.append(params.getStatus() == null?"":" and status = " + params.getStatus());
        sql.append(StringUtils.isEmpty(params.getNitoceTitle())?"":" and nitoce_title like '%" + params.getNitoceTitle() + "%'");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<NoticeTip> pageQuery = new PageQuery<NoticeTip>(params.getPage(),params.getLimit());
        PageQuery<NoticeTip> query =sqlManager.execute(sqlReady,NoticeTip.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<NoticeTip> list = noticeTipDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer noticeTipId) {
        String sql = "SELECT " +
                " a.*, " +
                " DATE_FORMAT( a.create_time, '%Y年%m月%d日 %H:%i' ) format_date " +
                "FROM " +
                " notice_tip a where a.notice_tip_id = ? " +
                "ORDER BY " +
                " a.create_time DESC";

        SQLReady sqlReady = new SQLReady(sql,noticeTipId);
        List<Map> query =sqlManager.execute(sqlReady,Map.class);
        return ResultUtil.success(query.size() > 0?query.get(0):null);
    }

    @Transactional
    public Result save(NoticeTip noticeTip, HttpServletRequest request) {
        if(StringUtils.isEmpty(noticeTip.getNoticeContent())){
            return ResultUtil.error(-1,"公告内容为空");
        }
        if(noticeTip.getStatus() == null){
            return ResultUtil.error(-1,"公告状态为空");
        }
        if(StringUtils.isEmpty(noticeTip.getNitoceTitle())){
            return ResultUtil.error(-1,"公告标题为空");
        }
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);

        noticeTip.setCreateUserId(sessionInfo.getUserId());
        noticeTip.setCreateTime(new Date());
        noticeTipDao.insert(noticeTip);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(NoticeTip noticeTip) {
        noticeTipDao.updateTemplateById(noticeTip);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(NoticeTip noticeTip) {
        noticeTipDao.updateTemplateById(noticeTip);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            noticeTipDao.deleteById(id);
        }
        return ResultUtil.success();
    }



}
